package workflow.element {
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.geom.Point;

import mx.core.DragSource;
import mx.managers.DragManager;

import spark.components.Image;
import spark.components.SkinnableContainer;

import workflow.constant.DragConst;
import workflow.event.SelectElementEvent;

public class ElementBase extends SkinnableContainer {
	
	//------------------------------
	// dragable
	//------------------------------

	private var _dragable:Boolean = false;

	public function get dragable():Boolean {
		return _dragable;
	}

	public function set dragable(dragable:Boolean):void {
		_dragable = dragable;
	}

	public function ElementBase() {
		addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
	}

	protected function getElementType():String {
		return null;
	}

	private function mouseDownHandler(e:MouseEvent):void {
		if (dragable) {
			startDragging(e);
		} else {
			e.stopPropagation();

			var point:Point = new Point(e.stageX, e.stageY);
			dispatchEvent(new SelectElementEvent(SelectElementEvent.SELECT, this, e.shiftKey, point));
		}
	}

	private function startDragging(e:MouseEvent):void {
		var initiator:ElementBase = e.currentTarget as ElementBase;

		var ds:DragSource = new DragSource();
		ds.addData(getElementType(), DragConst.ELEMENT_TYPE);
		ds.addData(initiator.mouseX, DragConst.XOFFSET);
		ds.addData(initiator.mouseY, DragConst.YOFFSET);
		ds.addData(initiator.width, DragConst.WIDTH);
		ds.addData(initiator.height, DragConst.HEIGHT);

		var bmdata:BitmapData = new BitmapData(initiator.width, initiator.height, true, 0x00000000);
		bmdata.draw(initiator);

		var dragImage:Image = new Image();
		dragImage.source = bmdata;

		DragManager.doDrag(initiator, ds, e, dragImage, 0, 0, 1);
	}

}

}
